<?php
/**
 * Date: 2024.8
 * Author: kahn
 * 本机号码授权一键登录
 */
use config\Config;
use service\uc\oauth\NativeMobileAuthClient;
use service\uc\UserService;
use service\uc\MobileAuthService;
use service\uc\SmsService;
use config\Response;

class NativeController extends Controller
{
    /**
     * 本机号码授权注册/登录
     */
    public function authAction()
    {
        $code = $this->_getParam('code', 'string');

        $mobile = (new NativeMobileAuthClient(Config::DONUT))->code2verifyinfo($code);

        if (!$mobile) {
            $this->_response(Response::NATIVE_AUTH_FAIL);
        }

        //检测并设置手机号登录每天使用次数相关
        if (!SmsService::checkLimit($mobile, $ret)) {
            $this->_response($ret);
        }
        SmsService::setLimit($mobile, Config::REGISTER_LOGIN_TYPE_MOBILE_AUTH);

        $ret = UserService::login($mobile, Config::REGISTER_LOGIN_TYPE_MOBILE_AUTH, $userId, $token);

        $this->_response($ret, ['token' => $token, 'mobile'=>$mobile]);
    }
}

